<?php
/**
 * Created by PhpStorm.
 * author :xupeng
 * Date: 2016/7/6
 * Time: 16:52
 */
namespace Home\Biz;

class ProductBiz
{

    /**
     * 查询产品数据
     * @param $product_name 产品名称
     * @param $material_no 物料编号
     * @param $sixCode 69码
     * @param $haier_material_code 海尔物料编号
     * @param $pageSize 煤业显示数量
     * @param $index 当前页码
     * @return result 查询结果集
     */
    public function queryProduct($product_name, $material_no, $sixCode, $haier_material_code, $pageSize, $index)
    {
        if (!empty ($product_name)) {
            $condition_array['product_name'] = array('like', "%" . $product_name . "%");
        }

        if (!empty ($material_no)) {
            $condition_array['material_no'] = array('like', "%" . $material_no . "%");
        }

        if (!empty ($sixCode)) {
            $condition_array['69_code'] = array('like', "%" . $sixCode . "%");
        }
        if (!empty ($haier_material_code)) {
            $condition_array['haier_material_no'] = array('like', "%" . $haier_material_code . "%");
        }

        $list_order_infos = M('product')->Field(array('69_code' => 'six_code', 'material_no' => 'material_no',
            'product_name' => 'product_name', 'cost_price' => 'cost_price', 'haier_material_no' => 'haier_material_no',
            'material_length' => 'material_length', 'material_width' => 'material_width', 'material_height' => 'material_height',
            'material_weight' => 'material_weight', 'material_volume' => 'material_volume', 'status' => 'status',
            'material_desc' => 'material_desc', 'product_id' => 'product_id','product_type' => 'product_type',
        	'product_group' => 'product_group','product_resolving' => 'product_resolving','product_part_group' => 'product_part_group',
        	'part_work_shop' => 'part_work_shop','product_part_model' => 'product_part_model'
        ))->where($condition_array)->page($index, $pageSize)->select();

        // 总记录数
        $totalCount = M('product')->where($condition_array)->count();

        // 总页数
        $totalPage = self::caculateTotalPage($totalCount, $pageSize);
        // 当前页记录数
        $currentCount = $list_order_infos . $pageSize;
        // 下一页页码
        $nextIndex = $index + 1;
        // 上一页页码 如果是第一页，页码仍然为1
        $preIndex = 1;
        if ($index > 1) {
            $preIndex = $index - 1;
        }

        $resultMap = array(
            'totalCount' => $totalCount,
            'totalPage' => $totalPage,
            'currentPage' => $currentCount,
            'nextIndex' => $nextIndex,
            'preIndex' => $preIndex,
            'index' => $index,
            'datas' => $list_order_infos
        );

        return $resultMap;

    }

    /**
     * 添加产品数据
     * @param $product 产品数据
     */
    public static function addProduct($product)
    {
    	//执行业务校验
        $error_msg = self::validate($product);
        //无拦截执行新增功能
        if (empty($error_msg)) {
        	$success_count = M('product')->add($product);
        }
        return $error_msg;
    }

    public static function updateProduct($product)
    {
    	//执行业务校验
        $error_msg = self::validate($product);
        //无拦截执行更新功能
        if (empty($error_msg)) {
	        $success_count = M('product')->where('product_id="%s"', array($product['product_id']))->save($product);
        }
        return $error_msg;
    }
    /**
     * Description: 同步产品
     * @version 2016年9月6日 下午2:14:33 
     * @author lin.yujiao@whaley.cn
     * @param array $product
     */
    private function syncProduct($product){
    	$post_data = array(
    		'productId' => $product['product_id']
    	);
    	$http_result = CommonBiz::httpPostJson(C( 'API.SYNC_PRODUCT' ), $post_data);
    	if($http_result['httpCode'] == 200){
    		$response = json_decode($http_result['response']);
    		//返回CODE:0成功 其余失败
    		if($response->code != 0){
    			return $response->message;
    		}
    	}else{
    		return '同步操作 HTTP状态:' . $http_result['httpCode'];
    	}
    }

    /**
     * 生成唯一主键
     * @return string
     */
    public static function generateProductId()
    {
        $seq = 10000000 + CommonBiz::getNewSeq('product_id');
        return 'PRO' . $seq;
    }

    public static function caculateTotalPage($totalCount, $pageSize)
    {

        $count = intval($totalCount / $pageSize);

        $countLeft = $totalCount % $pageSize;

        if ($countLeft != 0) {
            $count = $count + 1;
        }

        return $count;
    }

    private static function checkForm($product)
    {
        $price = $product['price'];
        if (!preg_match('/^(0|([1-9][0-9]{0,9}))(.[0-9]{1,3})?$/i', $price)) {
            return '产品价格格式错误';
        }
    }

    /**
     * 校验产品信息
     * @param $product
     */
    public static function validate($product)
    {
        //判空
        if (empty($product['product_name'])) {
            return "产品名称未填写";
        } else if (empty($product['material_desc'])) {
            return "产品型号未填写";
        } else if (empty($product['cost_price'])) {
            return "成本价未填写";
        } else if (empty($product['material_no'])) {
            return "客户物料号未填写";
        } else if (empty($product['material_length'])) {
            return "长度未填写";
        } else if (empty($product['material_width'])) {
            return "宽度未填写";
        } else if (empty($product['material_height'])) {
            return "高度未填写";
        } else if (empty($product['material_volume'])) {
            return "体积未填写";
        } else if (empty($product['material_weight'])) {
            return "重量未填写";
        } else if(empty($product['product_type'])){
        	return "产品分类必选";
        }
        //产品为主机时校验
        if($product['product_type'] == 1){
        	if(empty($product['product_group'])){
        		return "产品大类未填写";
        	}
        	if(empty($product['product_resolving'])){
        		return "产品分辨率未填写";
        	}
        	if(empty($product['product_size'])){
        		return "产品尺寸未填写";
        	}
        //产品为备件时校验
        }else if($product['product_type'] == 2){
        /*	if($product['material_no'] != $product['69_code']){
        		return "备件的客户物料号和69码必须相同";
        	}*/
        	if(empty($product['product_part_group'])){
        		return "备件小类未填写";
        	}
        	if(empty($product['product_part_model'])){
        		return "备件机型未填写";
        	}
        	if(empty($product['part_work_shop'])){
        		return "备件厂商编码未填写";
        	}
        }
 /*       if (!preg_match("/^69[0-9]*$/", $product['69_code'])) {
            return "69码格式不正确";
        }*/
        if(!empty($product['69_code'])){
            $condition_array['product_id'] = array('neq', $product['product_id']);
            $condition_array['69_code'] = array('eq', $product['69_code']);
            if((M('product', '')->where($condition_array)->count()) > 0){
                return '69码已存在,请修改后重新操作';
            }
        }


        if (!preg_match("/^(0|([1-9][0-9]{0,9}))(\.[0-9]{1,3})?$/", $product['material_length'])) {
            return "长度格式不正确";
        }
        if (!preg_match("/^(0|([1-9][0-9]{0,9}))(\.[0-9]{1,3})?$/", $product['material_width'])) {
            return "宽度格式不正确";
        }
        if (!preg_match("/^(0|([1-9][0-9]{0,9}))(\.[0-9]{1,3})?$/", $product['material_height'])) {
            return "高度格式不正确";
        }
        if (!preg_match("/^(0|([1-9][0-9]{0,9}))(\.[0-9]{1,3})?$/", $product['material_volume'])) {
            return "体积格式不正确";
        }
        if (!preg_match("/^(0|([1-9][0-9]{0,9}))(\.[0-9]{1,3})?$/", $product['material_weight'])) {
            return "重量格式不正确";
        }
       
    }

}

?>